package cn.com.egova.flavordemo;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    private ViewHolder holder = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        holder = new ViewHolder(rootView);
        setContentView(rootView);

        initView();
    }

    private void initView() {
        holder.pkgName.setText(getPackageName());
        // key值对应AndroidManifest.xml中的meta-data的name
        holder.WX_APPID.setText(getFlavorStringValue("wxAPPId"));
        holder.WX_SECRET.setText(getFlavorStringValue("wxSecret"));
        holder.QQ_APPID.setText(getFlavorStringValue("qqAPPId"));
        holder.QQ_APPKEY.setText(getFlavorStringValue("qqAppKey"));
        holder.API_KEY.setText(getFlavorStringValue("com.baidu.lbsapi.API_KEY"));
        holder.isShowGuidePage.setText(getFlavorBooleanValue("isShowGuidePage") + "");
        holder.JPUSH_PKGNAME.setText(getFlavorStringValue("jpushPkgName"));
        holder.JPUSH_APPKEY.setText(getFlavorStringValue("jpushAPPKey"));
        holder.JPUSH_CHANNEL.setText(getFlavorStringValue("jpushChannel"));
        holder.WEIBO_KEY.setText(getFlavorStringValue("weiboKey"));
        holder.WEIBO_SECRET.setText(getFlavorStringValue("weiboSecret"));
    }


    private String getFlavorStringValue(String metaKey) {
        try {
            PackageManager pm = getPackageManager();
            ApplicationInfo appInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            String value = appInfo.metaData.getString(metaKey);
            return TextUtils.isEmpty(value) ? "" : value;
        } catch (PackageManager.NameNotFoundException ignored) {
        }
        return "";
    }

    private Boolean getFlavorBooleanValue(String metaKey) {
        try {
            PackageManager pm = getPackageManager();
            ApplicationInfo appInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            Boolean value = appInfo.metaData.getBoolean(metaKey);
            return value;
        } catch (PackageManager.NameNotFoundException ignored) {
        }
        return false;
    }


    public static class ViewHolder {
        public View rootView;
        public TextView pkgName;
        public TextView WX_APPID;
        public TextView WX_SECRET;
        public TextView QQ_APPID;
        public TextView QQ_APPKEY;
        public TextView API_KEY;
        public TextView isShowGuidePage;
        public TextView JPUSH_PKGNAME;
        public TextView JPUSH_APPKEY;
        public TextView JPUSH_CHANNEL;
        public TextView WEIBO_KEY;
        public TextView WEIBO_SECRET;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.pkgName = (TextView) rootView.findViewById(R.id.pkgName);
            this.WX_APPID = (TextView) rootView.findViewById(R.id.WX_APPID);
            this.WX_SECRET = (TextView) rootView.findViewById(R.id.WX_SECRET);
            this.QQ_APPID = (TextView) rootView.findViewById(R.id.QQ_APPID);
            this.QQ_APPKEY = (TextView) rootView.findViewById(R.id.QQ_APPKEY);
            this.API_KEY = (TextView) rootView.findViewById(R.id.API_KEY);
            this.isShowGuidePage = (TextView) rootView.findViewById(R.id.isShowGuidePage);
            this.JPUSH_PKGNAME = (TextView) rootView.findViewById(R.id.JPUSH_PKGNAME);
            this.JPUSH_APPKEY = (TextView) rootView.findViewById(R.id.JPUSH_APPKEY);
            this.JPUSH_CHANNEL = (TextView) rootView.findViewById(R.id.JPUSH_CHANNEL);
            this.WEIBO_KEY = (TextView) rootView.findViewById(R.id.WEIBO_KEY);
            this.WEIBO_SECRET = (TextView) rootView.findViewById(R.id.WEIBO_SECRET);
        }

    }
}
